#include <iostream>
#include <cassert>
#include <string>
#include <thread>
#include <chrono>

#include "core/device_manager.h"
#include "core/packet.h"
#include "core/gateway_config.h"
#include "utils/logger.h"

class BasicFlowTest {
 public:
    void run() {
        std::cout << "=== Basic Flow Data Test ===" << std::endl;
        testDeviceInitialization();
        testPacketCapture();
        std::cout << "All tests passed!" << std::endl;
    }
 private:
    void testDeviceInitialization() {
        std::cout << "Testing device initialization... " << std::endl;
        auto& device_manager = DeviceManager::getInstance();
        GatewayConfig config;
        //        config.setDefaults();
        assert(device_manager.initialize(config));

        auto devices = device_manager.listInitializedDevice();
        std::string name = device_manager.listAvailableDevices()[0];
        std::cout << "First device: " << name << std::endl;
        assert(!devices.empty());
        std::cout << "Pass" << std::endl;
    }

    void testPacketCapture() {
        std::cout << "Testing packet capture... " << std::endl;
        auto& device_manager = DeviceManager::getInstance();
        std::string device_name = "lo";
        auto device = device_manager.getOrCreateDevice(device_name);
        device.get()->setPacketCallback([&](const Packet& packet){
            if (packet.length() > 0) {
                std::cout << "Packet length: " << packet.length() << std::endl;
            } else {
                std::cout << "Callback function called, but no data here." << std::endl;
            }
        });
        device.get()->startCapture();
        std::this_thread::sleep_for(std::chrono::milliseconds(120000));
        device.get()->stopCapture();
        std::cout << "Pass" << std::endl;
    }
};

int main() {
    Logger::getInstance().init(LogLevel::DEBUG);
    BasicFlowTest test;
    test.run();
    return 0;
}